package cn.yudao.product.controller;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.LoginContext;
import cn.yudao.pet.domain.PetDetail;
import cn.yudao.product.domain.ProductDetail;
import cn.yudao.product.dto.ProductPayDto;
import cn.yudao.product.service.IProductService;
import cn.yudao.product.domain.Product;
import cn.yudao.product.query.ProductQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.user.domain.Logininfo;
import cn.yudao.user.domain.User;
import cn.yudao.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;

    @Autowired
    public IUserService userService;

    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrupadte(@RequestBody Product product){
        try {
            if( product.getId()!=null)
                productService.upadte(product);
            else
                productService.save(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.LoadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query)
    {
        return productService.query(query);
    }


    @GetMapping("/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id){
        try {
            ProductDetail productDetail =  productService.loadDetailByPetId(id);
            return AjaxResult.me().setResult(productDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PostMapping("/dels")
    public AjaxResult rutchRom(@RequestBody Long[] ids){
        try {
            productService.rutchRom(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    @PostMapping("/onsale")
    public AjaxResult onSalePet(@RequestBody Long[] ids){
        try {
            productService.onSalePet(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    /**
     * @Title: onSalePet
     * @Description: 批量下架
     * @Author: Mr.Jia
     * @Version: 1.0
     * @Date:  2022/7/16 21:14
     * @Parameters:
     * @Return
     */
    @PostMapping("/offsale")
    public AjaxResult offSalePet(@RequestBody Long[] ids){
        try {
            productService.offSalePet(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }



    @PostMapping("/pay")
    public AjaxResult payProduct(@RequestBody ProductPayDto productPayDto, HttpServletRequest request)
    {
        try {
            // 设置当前用户的id值
            Logininfo logininfo = LoginContext.getLogininfo(request);
            // 根据logininfo的id查询到user
            User user = userService.loadByLogininfoId(logininfo.getId());
            productPayDto.setUserId(user.getId());
            productService.payProduct(productPayDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
}
